//
//  MainViewController.swift
//  weiboDemo
//
//  Created by zq on 2020/10/27.
//

import UIKit

/// 主控制器
class MainViewController: UITabBarController {
    //懒加载修饰变量 要指定类型,否则会推倒成可选类型
    lazy var composeButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViewControllers()
        // Do any additional setup after loading the view.
        setupComposeBtn()
        
    }
    /**
     portrait : 肖像:使用竖屏看肖像
     landscape: 使用横屏看风景画
     - 在主控制器设置支持方向之后,当前控制器及其子控制器都会支持
     - 如果播放视频 通常通过 modal 实现
     */
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    /// 发布微博
    // @objc 表示
    @objc private func composeStatus() -> () {
        print("发布微博")
        
        /**
         测试方向旋转
         设置supportInterfaceOrientations之后,使用push方式打开的viewController 都会遵守屏幕方向
         但是如果要播放视频,使用present的方式打开的viewController 不会遵守屏幕方向
         */
        let vc = InterfaceOrientationViewController()
        vc.view.backgroundColor = UIColor.cz_random()
        let v = UIView(frame: CGRect(x: 30, y: 30, width: 200, height: 200))
        v.backgroundColor = UIColor.orange
        vc.view.addSubview(v)
//        let nav = UINavigationController(rootViewController: vc)
        present(vc, animated: true, completion: nil)
    }
    
}

/// extension 相当于oc中的分类 这里面不能定义属性
extension MainViewController {
    
    private func setupComposeBtn(){
        tabBar.addSubview(composeButton)
        let count = CGFloat( viewControllers?.count ?? 0 )
        //将向内缩进的宽度减小,让按钮的宽度变大,盖住容错点,防止穿帮
        let w = tabBar.bounds.width / count - 1
        // return a rectangle that is smaller or larger then the source rectongle,with the same center point
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.y -= 20
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    /// 设置自控制器
    private func setupViewControllers(){
        guard let path = Bundle.main.path(forResource: "main.json", ofType: nil),
              let data = NSData.init(contentsOfFile: path) ,
              let array = try? JSONSerialization.jsonObject(with: data as Data, options: []) as? [[String : Any]]
              else {
            return
        }

        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        viewControllers = arrayM
    }
    
    
    /// 通过字典生成子控制器
    /// - Parameter dict: 字典信息:[clsName,title,imageName]
    /// - Returns:
    private func controller(dict:[String:Any]) -> UIViewController {
        // 1.取得地点的内容
        guard  let clsName = dict["clsName"] as? String,
               let title = dict["title"] as? String,
               let imageName = dict["imageName"] as? String,
               let cls = NSClassFromString(Bundle.main.nameSpace + "." + clsName ) as? BaseViewController.Type,
               let visitorInfo  = dict["visitorInfo"] as? [String : String]
        else {
            return UIViewController()
        }
        let vc = cls.init()
        vc.visitorInfo = visitorInfo
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置字体 大小 颜色
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.orange], for: UIControl.State.highlighted)
        //注意这里的默认字体是12
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14)], for: UIControl.State.normal)
        let nav = NavigationController(rootViewController: vc)
        return nav

    }
    
    
}
